<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 8/5/17
 * Time: 7:56 PM
 */
namespace ApiBundle\Controller\Platform;

use ApiBundle\Controller\BaseController;

use Symfony\Component\HttpFoundation\Request;

class AuthController extends BaseController
{
    /*
     * 登录
     */
    public function loginAction(Request $request)
    {
        $phone = $request->get('phone');
        $password = $request->get("password");

        $validations = $this->get("helper.validations");
        $strings = $this->get("helper.strings");

//        if(!$validations->isMobile($phone)){
//            return $this->jsonResponse(1,"手机号格式不正确");
//        }

        if(!$validations->isPassword($password)){
            return $this->jsonResponse(1,"密码为6~18位");
        }

        // TODO: 判断帐号是否注册
        $conn = $this->get("database_connection");
        $user = $conn->fetchAssoc("SELECT * FROM admin WHERE realname = '". $phone ."' AND admin_role_id = 1");
        if(!$user){
            return $this->jsonResponse(1,"帐号不存在");
        }

        $entry_password = $strings->encryptPassword($password,$user['salt']);


        if($user['password'] != $entry_password){
            return $this->jsonResponse(1,"密码错误", array('post' => $request->request->all(), 'user' => $user, 'entry_password' => $entry_password));
        }

        $token = $strings->generateToken($phone);
        $data = array(
            "token" => $token
        );
        $where = array(
            "id" => $user['id']
        );

        $conn->update("admin",$data,$where);

        $this->get("session")->set("plat_id",$user['id']);
        $this->get("session")->set("plat_name",$user['realname']);

        return $this->jsonResponse(0,"操作成功",array("token" => $token,"input_password"=>$entry_password,"user_password"=>$user['password']));

    }

    /*
     * 找回密码
     */
    public function forgetPasswordAction(Request $request)
    {
        $phone = $request->request->get("phone");
        $validation_code = $request->request->get("validation_code");
        $password = $request->request->get("password");
        $comfire_password = $request->request->get("comfire_password");

        $validations = $this->get("helper.validations");
        $serviceStrings = $this->get("helper.strings");

        if(!$phone || !$validations->isMobile($phone))
        {
            return $this->jsonResponse(1,"手机号格式不正确");
        }
        if(empty($validation_code)){
            return $this->jsonResponse(1,"请输入验证码");
        }

        // TODO: 短信验证
        $message_error = $this->checkSMSSalt($phone, $validation_code, 4, 4);
        if($message_error){
            return $this->jsonResponse(1,$message_error);
        }

        if(!$password || !$validations->isPassword($password))
        {
            return $this->jsonResponse(2,"密码为6~18位");
        }

        if(!$comfire_password || $comfire_password != $password){
            return $this->jsonResponse(2,"确认密码与密码不统一");
        }
        $conn = $this->get("database_connection");
        $select = $conn->fetchAssoc("SELECT * FROM admin WHERE phone = '". $phone ."' AND admin_role_id = 1");
        if(!$select){
            return $this->jsonResponse(1,"帐号不存在");
        }
        $salt = $serviceStrings->generateSalt(6, true);
        $encryptPassword = $serviceStrings->encryptPassword($password,$salt);
        $data = array(
            "password"=>$encryptPassword,
            'salt' => $salt
        );
        $where = array(
            "phone"=>$phone,
            "admin_role_id" => "1"
        );

        $update = $conn->update("admin",$data,$where);


        return $this->jsonResponse(0,"找回密码成功",$update);

    }

    /*
     * 退出登录
     */
    public function logoutAction()
    {
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }

        $conn = $this->get("database_connection");

        $conn->update("admin",array("token"=>NULL),array("id"=>$this->getPlatId()));

        $this->get("session")->clear();
        return $this->jsonResponse(0,"操作成功");
    }
}